﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using com.hyrentals.dbaccess;
using com.hyrentals.data;
using com.hyrentals.common;

namespace com.hyrentals.logic
{
    public class RVLogic
    {
        private RVDBAccess _RVDBAccess;

        public RVLogic()
        {
            _RVDBAccess = new RVDBAccess();
        }

        public bool InsertRV(string id, string ownerId, RVTypeEnum type, string modelStyle,
             DateTime madeYear, RVMadeByEnum madeBy, string made, byte sleeps,
             double length, string zip, int cityId, int stateId, RVAvailabilityTypeEnum availableType, bool isAllowedHandleBooking)
        {
            if (madeBy != RVMadeByEnum.Other)
            {
                made = null;
            }
            return _RVDBAccess.InsertRV(id, ownerId, type, modelStyle, madeYear,
                madeBy, made, sleeps, length, zip, cityId, stateId, availableType, isAllowedHandleBooking, RVStatus.NoReady, DateTime.UtcNow);
        }

        public IEnumerable<RV> GetRVByOwnerId(string ownerId)
        {
            return _RVDBAccess.GetRVByOwnerId(ownerId);
        }

        public IEnumerable<RV> GetAllRV(int page, int pageSize)
        {
            return _RVDBAccess.GetAllRV_Paging(page, pageSize);
        }

        public RV GetRVById(string id)
        {
            return _RVDBAccess.GetRVById(id);
        }

        public RV GetRVByIdAndOwnerId(string id, string ownerId)
        {
            return _RVDBAccess.GetRVByIdAndOwnerId(id, ownerId);
        }

        public bool SaveAutomotive(string id, byte[] basicFeatures, byte? overdriveTrans,
            byte[] electricalFeature, byte? engineType, double? fuelConsumption,
            string chassis, double? grossWeidht, double? tongueWeight)
        {
            RVAutomotiveBasicFeatureEnum[] basicFeatureArray = null;
            if(basicFeatures != null)
            {
                basicFeatureArray = new RVAutomotiveBasicFeatureEnum[basicFeatures.Length];
                for(int i = 0; i < basicFeatures.Length; ++i)
                {
                    basicFeatureArray[i] = (RVAutomotiveBasicFeatureEnum)basicFeatures[i];
                }
            }

            RVOverdriveTransmissionEnum? overdriveTransEnum = null;
            if(overdriveTrans != null)
                overdriveTransEnum = (RVOverdriveTransmissionEnum)overdriveTrans;

            RVAutomotiveElectricalFeatureEnum[] electricalFeatureArray = null;
            if(electricalFeature != null)
            {
                electricalFeatureArray = new RVAutomotiveElectricalFeatureEnum[electricalFeature.Length];
                for(int i = 0; i < electricalFeature.Length; ++i)
                {
                    electricalFeatureArray[i] = (RVAutomotiveElectricalFeatureEnum)electricalFeature[i];
                }
            }

            RVAutomotiveEngineTypeEnum? engineTypeEnum = null;
            if(engineType != null)
                engineTypeEnum = (RVAutomotiveEngineTypeEnum)engineType;

            RVAutomotive automotive = new RVAutomotive
            {
                BasicFeatures = basicFeatureArray,
                OverdriveTransmission = overdriveTransEnum,
                ElectricalFeatures = electricalFeatureArray,
                EngineType = engineTypeEnum,
                FuelConsumption = fuelConsumption,
                Chassis = chassis,
                GrossWeight = grossWeidht,
                TongueWeight = tongueWeight
            };

            return _RVDBAccess.UpdateAutomotive(id, automotive);
        }

        public bool SaveInterior(string id, byte sleeps, double? width, double? length,
            double? blackWaterTank, double? greyWaterTank, double? freshWaterTank,
            double? waterHeater, double? propaneTanks, byte[] kitchBathroomFeatures,
            byte[] bedFeatures)
        {
            RVInteriorKitchBathroomFeatureEnum[] kitchBathroomFeatureArray = null;
            if (kitchBathroomFeatures != null)
            {
                kitchBathroomFeatureArray = new RVInteriorKitchBathroomFeatureEnum[kitchBathroomFeatures.Length];
                for (int i = 0; i < kitchBathroomFeatures.Length; ++i)
                {
                    kitchBathroomFeatureArray[i] = (RVInteriorKitchBathroomFeatureEnum)kitchBathroomFeatures[i];
                }
            }

            RVInteriorBedFeatureEnum[] bedFeatureArray = null;
            if (bedFeatures != null)
            {
                bedFeatureArray = new RVInteriorBedFeatureEnum[bedFeatures.Length];
                for (int i = 0; i < bedFeatures.Length; ++i)
                {
                    bedFeatureArray[i] = (RVInteriorBedFeatureEnum)bedFeatures[i];
                }
            }

            RVInterior interior = new RVInterior
            {
                SpaceWidth = width,
                SpaceLenght = length,
                BlackWaterTank = blackWaterTank,
                GreyWaterTank = greyWaterTank,
                FreshWaterTank = freshWaterTank,
                WaterHeater = waterHeater,
                PropaneTanks = propaneTanks,
                KitchBathroomFeatures = kitchBathroomFeatureArray,
                BedFeatures = bedFeatureArray
            };

            return _RVDBAccess.UpdateInterior(id, interior, sleeps);
        }

        public bool SavePricing(string id, 
            decimal? rentalRateDaily, decimal? rentalRateWeekly, decimal? rentalRateMonthly, 
            int? milageFreeMilesDaily, int? milageFreeMilesWeekly, int? milageFreeMilesMonthly,
            decimal? milagePerExtraMileDaily, decimal? milagePerExtraMileWeekly, decimal? milagePerExtraMileMonthly,
            decimal? depositNonrefundableSecurityDownPayment,
            decimal? depositRefundableDamageProtection,
            decimal? depositRefundableCleaningCharge,
            decimal? lateChargePerHour, IEnumerable<RVItermizedCharge> itemizedCharges)
        {
            RVPricing pricing = new RVPricing()
            {
                RentalRateDaily = rentalRateDaily,
                RentalRateWeekly = rentalRateWeekly,
                RentalRateMonthly = rentalRateMonthly,
                MilageFreeMilesDaily = milageFreeMilesDaily,
                MilageFreeMilesWeekly = milageFreeMilesWeekly,
                MilageFreeMilesMonthly = milageFreeMilesMonthly,
                MilagePerExtraMileDaily = milagePerExtraMileDaily,
                MilagePerExtraMileWeekly = milagePerExtraMileWeekly,
                MilagePerExtraMileMonthly = milagePerExtraMileMonthly,
                DepositNonrefundableSecurityDownPayment = depositNonrefundableSecurityDownPayment,
                DepositRefundableDamageProtection = depositRefundableDamageProtection,
                DepositRefundableCleaningCharge = depositRefundableCleaningCharge,
                LateChargePerHour = lateChargePerHour,
                OtherCharges = itemizedCharges == null ? null : itemizedCharges.ToArray()
            };

            return _RVDBAccess.UpdatePricing(id, pricing);
        }

        public bool SaveOverview(string id, string title, string summary, byte type,
            string modelStyle, DateTime madeYear, byte madeBy, string made, bool isAllowedHandleBooking)
        {
            return _RVDBAccess.UpdateOverview(id, title, summary, type,
            modelStyle, madeYear, madeBy, made, isAllowedHandleBooking);
        }

        public bool SaveCalendar(string id, RVAvailabilityTypeEnum availability, IEnumerable<RVAvailableDateInterval> dateIntervals)
        {
            if (availability == RVAvailabilityTypeEnum.Always)
            {
                dateIntervals = null;
            }
            return _RVDBAccess.UpdateCalendar(id, availability, dateIntervals);
        }

        public bool SavePhoto(Photo photo)
        {
            return _RVDBAccess.InsertPhoto(photo);
        }

        public IEnumerable<Photo> GetPhotoByRVId(string RVId)
        {
            return _RVDBAccess.GetPhotoByRVId(RVId);
        }

        public bool DeletePhoto(string photoId, string RVId)
        {
            return _RVDBAccess.DeletePhoto(photoId, RVId);
        }

        public bool UpdatePhoto(string ownerId, string photoId, string RVId, string description)
        {
            return _RVDBAccess.UpdatePhoto(ownerId, photoId, RVId, description);
        }

        public bool SaveAddress(string RVId, string zip, string address)
        {
            return _RVDBAccess.UpdateAddress(RVId, zip, address);
        }

        public bool SaveCoverPhoto(string RVId, string coverPhotoId)
        {
            return _RVDBAccess.UpdateCoverPhoto(RVId, coverPhotoId);
        }
    }
}
